package com.qf.test;

import com.alibaba.fastjson.JSON;
import com.qf.utils.ESUtil;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 操作文档
 */
public class DocumentDemo {
    /*
    //1. 创建指定的Request对象，并设置索引名称和doc_id

    //2. 设置添加的数据给Request对象

    //3. 通过连接对象将Request发送给ES，并接受Response

    //4. 通过Response判断添加是否成功
     */
    // 连接对象 http://10.9.41.192:9200   /book/_doc/1
    // 请求体中的JSON

    //1. 添加文档到ES
    @Test
    public void addDoc() throws IOException {
        //1. 创建指定的Request对象，并设置索引名称和doc_id
        IndexRequest request = new IndexRequest();
        request.index("book");
        request.id("9");

        //2. 设置添加的数据给Request对象
        Map<String,Object> book = new HashMap<>();
        book.put("name","斗罗大陆");
        book.put("author","唐家三少");
        book.put("count",1312423542);
        book.put("date",new Date());
        request.source(JSON.toJSONString(book), XContentType.JSON);

        //3. 通过连接对象将Request发送给ES，并接受Response
        IndexResponse response = ESUtil.getClient().index(request, RequestOptions.DEFAULT);

        //4. 通过Response判断添加是否成功
        System.out.println(response.getResult().toString());
    }

    //2. 修改文档数据
    @Test
    public void updateDoc() throws IOException {
        //1. 创建指定的Request对象，并设置索引名称和doc_id
        UpdateRequest request = new UpdateRequest();
        request.index("book");
        request.id("1");

        //2. 设置添加的数据给Request对象
        Map<String,Object> doc = new HashMap<>();
        doc.put("name","地蚕土豆片!");
        request.doc(doc);

        //3. 通过连接对象将Request发送给ES，并接受Response
        UpdateResponse response = ESUtil.getClient().update(request, RequestOptions.DEFAULT);

        //4. 通过Response判断添加是否成功
        System.out.println(response.getResult().toString());
    }

    //3. 删除文档数据
    @Test
    public void deleteDoc() throws IOException {
        //1. 创建指定的Request对象，并设置索引名称和doc_id
        DeleteRequest request = new DeleteRequest();
        request.index("book");
        request.id("1");

        //2. 设置添加的数据给Request对象
        // 木有

        //3. 通过连接对象将Request发送给ES，并接受Response
        DeleteResponse resp = ESUtil.getClient().delete(request, RequestOptions.DEFAULT);

        //4. 通过Response判断添加是否成功
        System.out.println(resp.getResult().toString());
    }

    //4. 批量操作数据
    @Test
    public void blukDoc() throws IOException {
        //1. 创建指定的Request对象，
        BulkRequest request = new BulkRequest();

        //2. 设置添加的数据给Request对象
        DeleteRequest deleteRequest1 = new DeleteRequest("book","1");
        DeleteRequest deleteRequest2 = new DeleteRequest("book","2");
        DeleteRequest deleteRequest3 = new DeleteRequest("book","3");
        DeleteRequest deleteRequest4 = new DeleteRequest("book","4");
        request.add(deleteRequest1).add(deleteRequest2).add(deleteRequest3).add(deleteRequest4);

        //3. 通过连接对象将Request发送给ES，并接受Response
        BulkResponse resp = ESUtil.getClient().bulk(request, RequestOptions.DEFAULT);

        //4. 通过Response判断添加是否成功
        for (BulkItemResponse item : resp.getItems()) {
            System.out.println(item.getResponse().getResult().toString());
        }
    }

}
